var casper = require("casper").create({
	clientScript: [
		'includes/jquery.js',
		'includes/underscore.js'
	],
	pageSettings: {
		loadImages: false,
		loadPlugins: false
	},
	verbose:true,
	logLevel: "debug"
});
casper.start('https://www.google.com/?gws_rd=cr,ssl',function() {
	this.capture('google.png', {
        top: 0,
        left: 0,
        width: 500,
        height: 400
    });
});

casper.thenEvaluate(function(term) {
    document.querySelector('input[name="q"]').setAttribute('value', term);
    document.querySelector('form[name="f"]').submit();
}, 'CasperJS');

casper.then(function() {
    // Click on 1st result link
    this.click('h3.r a');
});

casper.then(function() {
    // Click on 1st result link
    this.capture('casper.png', {
        top: 0,
        left: 0,
        width: 500,
        height: 400
    });
    this.click('a',0,50);
});

casper.then(function() {
    // Click on 1st result link
    this.capture('foo.jpg', undefined, {
        format: 'jpg',
        quality: 75
    });
    this.click('h3.r a',"50%","50%");
});


casper.then(function() {
    console.log('clicked ok, new location is ' + this.getCurrentUrl());
});

casper.run();